/** 
 * DefaultTossParser.java Created by iweerarathna on Aug 30, 2011.
 * 
 * Copyright (c) 2011 Virtusa Corporation. All Rights Reserved.<br><br>. 
 * 
 */
package org.uim.cricket.analyzer.parsers.match.impl;

import org.uim.cricket.analyzer.parsers.match.TossParsable;
import org.uim.cricket.entities.match.CricketMatch;
import org.uim.cricket.entities.match.Toss;

/**
 * TODO Class description goes here.
 * @version 
 * @author iweerarathna
 */
public class DefaultTossParser implements TossParsable {

    @Override
    public Toss parseToss(String source, CricketMatch match) {
        if (source == null || source.length() == 0) return null;
        Toss toss = new Toss();
        
        int pos = source.indexOf("</b>");
        if (pos < 0) return null;
        source = source.substring(pos + 4).trim();
        
        pos = source.indexOf(',');
        if (pos < 0) return null;

        toss.setTossWon(source.substring(0, pos).trim());
        if (source.indexOf(" bat") >= 0) {
            toss.setFielded(false);
        } else {
            toss.setFielded(true);
        }
        
        return toss;
    }

}
